#include "highlight.h"
#include <QRegularExpression>
#include <QTextDocument>
#include <qdebug.h>
#include <iostream>
#include <QProcess>
#include <fstream>
#include <QColorDialog>
#include <QPushButton>
#include <QMessageBox>
#include "color.h"

QTextCharFormat labelFormat;
QTextCharFormat Title1Format;
QTextCharFormat Title2Format;
QTextCharFormat Title3Format;
QTextCharFormat Title4Format;
QTextCharFormat Title5Format;
QTextCharFormat Title6Format;
QTextCharFormat QuoteFormat;
QTextCharFormat ListFormat;
QTextCharFormat OlistFormat;
QTextCharFormat LinkFormat;
QTextCharFormat emFormat,strongFormat;
QTextCharFormat imgFormat;
QTextCharFormat codeFormat;
QTextCharFormat delFormat;
QTextCharFormat tableFormat;

Rule table(Qregex("\\|"),&tableFormat);
Rule Title1(Qregex("^#{1} .*"),&Title1Format);
Rule Title2(Qregex("^#{2} .*"),&Title2Format);
Rule Title3(Qregex("^#{3} .*"),&Title3Format);
Rule Title4(Qregex("^#{4} .*"),&Title4Format);
Rule Title5(Qregex("^#{5} .*"),&Title5Format);
Rule Title6(Qregex("^#{6,} .*"),&Title6Format);
Rule Quote(Qregex("^>.*"),&QuoteFormat);
Rule List(Qregex("^[\\s]*[-*+]{1} "),&ListFormat);
Rule Olist(Qregex("[\\s]*[\\d]+\\. "),&OlistFormat);
Rule Link(Qregex("\\[(.+?)\\]\\((.+?)\\)"),&LinkFormat);
Rule em(Qregex("\\*"),&emFormat);
Rule strong(Qregex("\\*\\*"),&strongFormat);
Rule img(Qregex("!\\[(.+?)\\]\\((.+?)\\)"),&imgFormat);
Rule code(Qregex("`(.+?)`"),&codeFormat);
Rule del(Qregex("~~(.+?)~~"),&delFormat);
Rule label(Qregex("<.+?>"),&labelFormat);

QString converRGB16HexStr(QColor _color) {
    //r的rgb转化为16进制
    QString redStr = QString("%1").arg(_color.red(),2,16,QChar('0'));
    //g的rgb转化为16进制
    QString greenStr = QString("%1").arg(_color.green(),2,16,QChar('0'));
    //b的rgb转化为16进制
    QString blueStr = QString("%1").arg(_color.blue(),2,16,QChar('0'));
    //将各rgb的拼接在一起形成#000000
    QString hexStr = "#"+redStr+greenStr+blueStr;
    //返回hexStr
    return hexStr;
}

/*converRGB16HexStr(tabelFormat.foreground().color()).toStdString()*/

void highlight::init(double size){
    std::ifstream f("highlight.gh");
    if(f.good()){
        std::string table,labelColor,labelFamily,Title,Quote,List,Olist,Link,img,em,strong,codeColor,codeFamily;
        getline(f,table);
        std::string temp;
        getline(f,temp);
        labelColor = QString::fromStdString(temp).split('/')[0].toStdString();
        labelFamily = QString::fromStdString(temp).split('/')[1].toStdString();
        getline(f,Title);
        getline(f,Quote);
        getline(f,List);
        getline(f,Olist);
        getline(f,Link);
        getline(f,img);
        getline(f,em);
        getline(f,strong);
        getline(f,temp);
        codeColor = QString::fromStdString(temp).split('/')[0].toStdString();
        codeFamily = QString::fromStdString(temp).split('/')[1].toStdString();
        tableFormat.setForeground(QColor(QString::fromStdString(table)));
        labelFormat.setFontFamily(QString::fromStdString(labelFamily));
        labelFormat.setForeground(QColor(QString::fromStdString(labelColor)));
        Title1Format.setFontWeight(QFont::Bold);
        Title1Format.setFontPointSize(size + 25);
        Title1Format.setForeground(QColor(QString::fromStdString(Title)));
        Title2Format.setFontWeight(QFont::Bold);
        Title2Format.setFontPointSize(size + 23);
        Title2Format.setForeground(QColor(QString::fromStdString(Title)));
        Title3Format.setFontWeight(QFont::Bold);
        Title3Format.setFontPointSize(size + 22);
        Title3Format.setForeground(QColor(QString::fromStdString(Title)));
        Title4Format.setFontWeight(QFont::Bold);
        Title4Format.setFontPointSize(size + 21);
        Title4Format.setForeground(QColor(QString::fromStdString(Title)));
        Title5Format.setFontWeight(QFont::Bold);
        Title5Format.setFontPointSize(size + 19);
        Title5Format.setForeground(QColor(QString::fromStdString(Title)));
        Title6Format.setFontWeight(QFont::Bold);
        Title6Format.setFontPointSize(size + 17);
        Title6Format.setForeground(QColor(QString::fromStdString(Title)));
        QuoteFormat.setForeground(QColor(QString::fromStdString(Quote)));
        ListFormat.setFontWeight(QFont::Bold);
        OlistFormat.setFontWeight(QFont::Bold);
        OlistFormat.setForeground(QColor(QString::fromStdString(Olist)));
        ListFormat.setForeground(QColor(QString::fromStdString(List)));
        LinkFormat.setForeground(QColor(QString::fromStdString(Link)));
        imgFormat.setForeground(QColor(QString::fromStdString(img)));
        emFormat.setForeground(QColor(QString::fromStdString(em)));
        strongFormat.setFontWeight(QFont::Black);
        strongFormat.setForeground(QColor(QString::fromStdString(strong)));
        codeFormat.setFontFamily(QString::fromStdString(codeFamily));
        codeFormat.setForeground(QColor(QString::fromStdString(codeColor)));
        delFormat.setFontStrikeOut(1);
    }else{
        std::ofstream of("highlight.gh",std::ios::trunc);
        if(of.good()){
            of << "#620c74\n#620c74/monospace\n#F92772\n#a9a9a9\n#fe639c\n#fe639c\n#1E90FF\n#1E90FF\n#069ef9\n#B22222\n#620c74/monospace\n";
            of.close();
            std::ifstream f("highlight.gh");
            if(f.good()){
                std::string table,labelColor,labelFamily,Title,Quote,List,Olist,Link,img,em,strong,codeColor,codeFamily;
                getline(f,table);
                std::string temp;
                getline(f,temp);
                labelColor = QString::fromStdString(temp).split('/')[0].toStdString();
                labelFamily = QString::fromStdString(temp).split('/')[1].toStdString();
                getline(f,Title);
                getline(f,Quote);
                getline(f,List);
                getline(f,Olist);
                getline(f,Link);
                getline(f,img);
                getline(f,em);
                getline(f,strong);
                getline(f,temp);
                codeColor = QString::fromStdString(temp).split('/')[0].toStdString();
                codeFamily = QString::fromStdString(temp).split('/')[1].toStdString();
                std::cout << "loadConyo" << std::endl;
                tableFormat.setForeground(QColor(QString::fromStdString(table)));
                labelFormat.setFontFamily(QString::fromStdString(labelFamily));
                labelFormat.setForeground(QColor(QString::fromStdString(labelColor)));
                Title1Format.setFontWeight(QFont::Bold);
                Title1Format.setFontPointSize(size + 25);
                Title1Format.setForeground(QColor(QString::fromStdString(Title)));
                Title2Format.setFontWeight(QFont::Bold);
                Title2Format.setFontPointSize(size + 23);
                Title2Format.setForeground(QColor(QString::fromStdString(Title)));
                Title3Format.setFontWeight(QFont::Bold);
                Title3Format.setFontPointSize(size + 22);
                Title3Format.setForeground(QColor(QString::fromStdString(Title)));
                Title4Format.setFontWeight(QFont::Bold);
                Title4Format.setFontPointSize(size + 21);
                Title4Format.setForeground(QColor(QString::fromStdString(Title)));
                Title5Format.setFontWeight(QFont::Bold);
                Title5Format.setFontPointSize(size + 19);
                Title5Format.setForeground(QColor(QString::fromStdString(Title)));
                Title6Format.setFontWeight(QFont::Bold);
                Title6Format.setFontPointSize(size + 17);
                Title6Format.setForeground(QColor(QString::fromStdString(Title)));
                QuoteFormat.setForeground(QColor(QString::fromStdString(Quote)));
                ListFormat.setFontWeight(QFont::Bold);
                OlistFormat.setFontWeight(QFont::Bold);
                OlistFormat.setForeground(QColor(QString::fromStdString(Olist)));
                ListFormat.setForeground(QColor(QString::fromStdString(List)));
                LinkFormat.setForeground(QColor(QString::fromStdString(Link)));
                imgFormat.setForeground(QColor(QString::fromStdString(img)));
                emFormat.setForeground(QColor(QString::fromStdString(em)));
                strongFormat.setFontWeight(QFont::Black);
                strongFormat.setForeground(QColor(QString::fromStdString(strong)));
                codeFormat.setFontFamily(QString::fromStdString(codeFamily));
                codeFormat.setForeground(QColor(QString::fromStdString(codeColor)));
                delFormat.setFontStrikeOut(1);
                f.close();
            }
        }else{
            return;
        }
    }
}

void highlight::writeDocu(){

}

highlight::highlight(double size,QTextDocument* father):QSyntaxHighlighter(father)
{
    this->size = size;
    init(size);
}


void highlight::highlightBlock(const QString &text){
    foreach (const Rule &rule, highRules) {
        QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
        while (matchIterator.hasNext()) {
            QRegularExpressionMatch match = matchIterator.next();
            setFormat(match.capturedStart(), match.capturedLength(), *rule.format);
        }
    }
}
